
#include <iostream>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <unistd.h>

#define Path "."
int getShmId(int size){
    key_t key=ftok(Path,'a');
    if(key==-1){
        perror("ftok");
        return -1;
    }
    int shmid=shmget(key,size,0666|IPC_CREAT);
    if(shmid==-1){
        perror("shmget");
        return -1;
    }
    return shmid;
}
void* comShm(int shmid,int size){
    void* shm_ptr=shmat(shmid,NULL,0);
    if(shm_ptr==(void*)-1){
        perror("shmat");
        return NULL;
    }
    return shm_ptr;
}
void* createShm(int shmid,int size){
    void* shm_ptr=comShm(shmid,1024);
    if(shm_ptr==NULL){
        perror("shmget");
        return NULL;
    }
    return shm_ptr;
}
void* getShm(int shmid,int size){
    void* shm_ptr=comShm(shmid,1024);
    if(shm_ptr==NULL){
        perror("shmget");
        return NULL;
    }
    return shm_ptr;
}
void delShm(int shmid){
    if(shmctl(shmid,IPC_RMID,NULL)==-1){
        perror("shmctl");
    }
}
